#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>

#include "sock.h"

static const int8_t *local_ip = "192.168.10.144";
static const uint16_t local_port = 6666;

static const int8_t *server_ip = "192.168.10.57";
static const uint16_t server_port = 8888;

int main(int argc, char *argv[])
{
    int32_t sock_fd = -1;
    uint8_t msg[64] = {0};
    int8_t src_ip[16] = {0};
    uint16_t src_port = 0;
    int32_t ret = 0;

    sock_fd = udp_socket_create();
    if(0 > sock_fd)
    {
        printf("udp socket create failed\n");
        return -1;
    }

    ret = udp_socket_bind_ip_port(sock_fd, local_ip, local_port);
    if(0 > ret)
    {
        printf("bind to %s:%u failed\n", local_ip, local_port);
        goto exit;
    }

    ret = udp_socket_connect(sock_fd, server_ip, server_port);
    if(0 > ret)
    {
        printf("bind to %s:%u failed\n", local_ip, local_port);
        goto exit;
    }

    while(1)
    {
        udp_socket_recv(sock_fd, msg, sizeof(msg));
        printf("receive from %s:%u, '%s'\n", src_ip, src_port, msg);

        memset(msg, 0, sizeof(msg));

        sleep(1);
    }

exit:
    udp_socket_destroy(sock_fd);

    return 0;
}
